package cn.xueshishasha.managers;

import java.util.Set;

import org.hibernate.Session;

import cn.xueshishasha.exceptions.ReplyFailedException;
import cn.xueshishasha.hibernateutils.HibernateSessionFactory;
import cn.xueshishasha.models.Message;
import cn.xueshishasha.models.Reply;
import cn.xueshishasha.models.User;

public class ReplyManager implements IReplyManager {

	public void reply(Reply reply, int msgId, int userId)throws ReplyFailedException {
		
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();//.getSession();
		session.beginTransaction();
		try{
			Message message = (Message)session.load(Message.class, msgId);
			User publisher = (User)session.load(User.class, userId);
			
			Set<Reply> replys=message.getReplys();
			reply.setPublisher(publisher);
			replys.add(reply);
			
			session.update(message);
			
			session.getTransaction().commit();
		}catch(RuntimeException e){
			e.printStackTrace();
			session.getTransaction().rollback();
			throw new ReplyFailedException();
		}

	}

}
